﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Exam.Bussiness;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class ExamSegment
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                bool CheckAllowAccess = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, ExamPermission.StaticId, ExamPermission.funcExamManage, ExamPermission.StaticId, WebSessionUtility.GetCurrentLoginUser().UserId, false);
                if(!CheckAllowAccess)
                    throw new Exception("Tham số không hợp lệ!");

                StringBuilder RetHtml = new StringBuilder();
                RetHtml.Append(

                #region Javascript

"<script language=javascript> \r\n" +

                    "    function ClientLoadRecord(Id)\r\n" +
                   "    {\r\n" +
                   "        document.getElementById('txtSelectedUserType').value=Id;\r\n" +
                   "        ClientReading();\r\n" +
                   "    }\r\n" +
                    "    function ClientReading()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        UserType = document.getElementById('txtSelectedUserType').value;\r\n" +
                   "        Keyword = document.getElementById(\"txtKeyword\").value; \r\n" +
                   "        AjaxOut = Portal.Exam.Render.ExamSegment.ServerSideReadingExamSegment(SiteId, UserType, 'exam',1,'',0).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divExamSegmentMain').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +

                    "function ClientDeleteExamSegment(ExamSegmentId) { \r\n" +
                    "    SiteId='" + SiteId + "';\r\n" +
                    "    Msg = Portal.Exam.Render.ExamSegment.ServerSideCheckDeleteExamSegment(SiteId).value; \r\n" +
                    "    if (Msg.indexOf(\"ERROR\") != -1) { \r\n" +
                    "        alert(Msg); \r\n" +
                    "        return; \r\n" +
                    "    } \r\n" +

                    "    Msg = 'Bạn đã chắc chắn xóa chưa?'; \r\n" +
                    "    if (confirm(Msg) == false) return; \r\n" +
                    "    RetValue = Portal.Exam.Render.ExamSegment.ServerSideDeleteExamSegment(SiteId, ExamSegmentId).value; \r\n" +
                    "    if (RetValue.indexOf(\"ERROR\") != -1) { \r\n" +
                    "        alert(RetValue); \r\n" +
                    "        return; \r\n" +
                    "    } \r\n" +
                    "    alert(RetValue); \r\n" +
                    "    RealJumpPageExamSegment(); \r\n" +
                    "} \r\n" +


                    "function JumPageExamSegment(PageIndex) { \r\n" +
                    "   document.getElementById('txtPageIndex').value=PageIndex;\r\n" +
                    "   document.getElementById(\"divProcessing\").innerHTML = 'Đang đọc dữ liệu từ máy chủ...'; \r\n" +
                    "   setTimeout(\"RealJumpPageExamSegment()\", 10); \r\n" +
                    "} \r\n" +

                    "function RealJumpPageExamSegment() { \r\n" +
                    "    SiteId='" + SiteId + "';\r\n" +
                    "    UserType = document.getElementById('txtSelectedUserType').value;\r\n" +
                    "    PageIndex = document.getElementById(\"txtPageIndex\").value; \r\n" +
                    "    Keyword = document.getElementById(\"txtKeyword\").value; \r\n" +
                    "    ExamType = document.getElementById(\"drpSelectExamType\").value; \r\n" +
                    "    Active = document.getElementById(\"drpSelectActive\").value; \r\n" +
                    "    AjaxOut = Portal.Exam.Render.ExamSegment.ServerSideReadingExamSegment(SiteId,UserType, ExamType, Active, Keyword, PageIndex).value; \r\n" +
                    "    if (AjaxOut.Error) { \r\n" +
                    "        alert(AjaxOut.InfoMessage); \r\n" +
                    "        return; \r\n" +
                    "    } \r\n" +
                    "    document.getElementById(\"divProcessing\").innerHTML = \"\"; \r\n" +
                    "    document.getElementById(\"divExamSegmentMain\").innerHTML = AjaxOut.HtmlContent; \r\n" +
                    "} \r\n" +


                    "    function ClientAddExamSegment()\r\n" +
                    "    {\r\n" +
                    "         SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                    "         if(SelectedNode==null)\r\n" +
                    "         {\r\n" +
                    "            alert('Chưa xác định vào nhóm cần thêm!');\r\n" +
                    "            return;\r\n" +
                    "         }\r\n" +

                    "        AccountType = SelectedNode.data.href;\r\n" +
                    "        if(AccountType=='root')\r\n" +
                    "        {\r\n" +
                    "            alert('Chưa xác định vào nhóm cần thêm! Không thể thêm vào nhóm gốc!');\r\n" +
                    "            return;\r\n" +
                    "        }\r\n" +
                    "        window.open('AddExamSegment.aspx?AccountType='+AccountType,'_self');\r\n" +
                    "    }\r\n" +

                    "function ClientUpdateExamSegment(ExamSegmentId) { \r\n" +
                    "    SiteId='" + SiteId + "';\r\n" +
                    "    Url = Portal.Exam.Render.ExamSegment.GetUpdateExamUrl(SiteId,ExamSegmentId).value; \r\n" +
                    "    if (Url.indexOf(\"ERROR\") != -1) { \r\n" +
                    "        alert(Url); \r\n" +
                    "        return; \r\n" +
                    "    } \r\n" +
                    "    window.open(Url, '_self'); \r\n" +
                    "} \r\n" +

                    " function ClientStatus(ExamSegmentId,Status)\r\n" +
                    " {\r\n" +
                    "    SiteId='" + SiteId + "';\r\n" +
                    "    AjaxOut = Portal.Exam.Render.ExamSegment.ServerSideSetStatus(SiteId,ExamSegmentId, Status).value; \r\n" +
                    "    if (AjaxOut.Error) { \r\n" +
                    "        alert(AjaxOut.InfoMessage); \r\n" +
                    "        return; \r\n" +
                    "    } \r\n" +
                    "    document.getElementById('spanButton'+ExamSegmentId).innerHTML=AjaxOut.HtmlContent;\r\n" +
                    " }\r\n" +
                    "</script>\r\n" +
                #endregion

 " <input id=\"txtSelectedUserType\" type=\"hidden\">\r\n" +
                    "<input id=txtPageIndex type=hidden value=\"0\">\r\n" +

                    " <div class=FormFunction id=divListForm>\r\n" +
                    " <div class=DatabaseFunction>\r\n" +
                    "        <ul>\r\n");
                bool AllowMan = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, ExamPermission.StaticId, ExamPermission.funcExamManage, ExamPermission.StaticId, WebSessionUtility.GetCurrentLoginUser().UserId, false);
                if (AllowMan)
                {
                    RetHtml.Append(
                    "            <li onclick=\"javascript:ClientAddExamSegment();\"><img class=image src=\"" + IconRoot + "/Add.png\"><div>Thêm mới</div></li>\r\n");
                }
                RetHtml.Append(
                "            <li onclick=\"javascript:JumPageExamSegment(0);\"><img class=image src=\"" + IconRoot + "/Refresh.png\"><div>Đọc lại</div></li>\r\n" +
                "            <li onclick=\"javascript:window.open('../../folder.aspx?folder=exam','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                "        </ul>\r\n" +
                "  </div>\r\n" +

                "    <div id=\"divListItems\">\r\n" +
                "        <table>\r\n" +
                "        <tr>\r\n" +
                "            <td class=col1>\r\n" +
                "                <div class=CssTreeView>\r\n" +
                                    ExamUltility.DrawTreeViewByAccountType(SiteId, WebSessionUtility.GetCurrentLoginUser().UserId).HtmlContent +
                "                </div>\r\n" +
                "            </td>\r\n" +
                "            <td class=col2>\r\n" +
                "                <div id=divExamSegmentMain>\r\n" +
                                    ServerSideReadingExamSegment(SiteId, "", "exam", 1, "", 0).HtmlContent +
                "                </div>\r\n" +
                "            </td>\r\n" +
                "        </tr>\r\n" +
                "        </table>\r\n" +
                "    </div>\r\n" +
                "</div>");

                OAjaxOut.HtmlContent = RetHtml.ToString();
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideReadingExamSegment(
            string SiteId,
            string UserType,
            string ExamType,
            int Active,
            string Keyword,
            int CurrentPageIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                DataTable dtExamSegment = new ExamSegmentBll().ReadingExamSegmentForAccountType(SiteId, UserType, ExamType, Active, Keyword);
                ReturnPaging OReturnPaging = WebPaging.GetPaging(dtExamSegment.Rows.Count, CurrentPageIndex, 25, 10, "JumPageExamSegment");                

                string SelectActive =
                    "<select id=drpSelectActive>\r\n" +
                    "   <option value=\"1\">Đang sử dụng</option>\r\n" +
                    "   <option value=\"0\">Đã xong</option>\r\n" +
                    "</select>\r\n";
                string SelectExamType =
                    "<select id=drpSelectExamType>\r\n" +
                    "   <option value=\"exam\">Thi thật</option>\r\n" +
                    "   <option value=\"test\">Thi thử</option>\r\n" +
                    "</select>\r\n";

                StringBuilder RetHtml = new StringBuilder();
                RetHtml.Append(
                    "  <div class=\"tabFunction\">Hình thức: " + SelectExamType + " / Trạng thái: " + SelectActive + " Từ khóa: <input id=txtKeyword value=\"" + Keyword + "\"><input type=button class=button value=\"Lọc\" onclick=JumPageExamSegment(0);></div>\r\n" +
                    "  <div id=divProcessing></div>" +
                    "   <div class=CssNoData>Số bản ghi thỏa mãn điều kiện tìm: " + dtExamSegment.Rows.Count.ToString("#,##0") + "</div>" +
                        OReturnPaging.PagingText +
                    "   <div class=\"tabcontent\">\r\n" +
                    "   <table>" +
                    "       <tr>" +
                    "           <th class=thIndex>STT</th>" +
                    "           <th>Kỳ thi</th>" +
                    "           <th>Từ ngày</th>" +
                    "           <th>Đến ngày</th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "       </tr>");

                for (int iIndex = OReturnPaging.StartIndex; iIndex < OReturnPaging.EndIndex; iIndex++)
                {
                    string ExamSegmentId = (string)dtExamSegment.Rows[iIndex]["ExamSegmentId"];
                    bool AllowAccess = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, ExamSegmentPermission.StaticId, ExamSegmentPermission.funcAccess, ExamSegmentId, WebSessionUtility.GetCurrentLoginUser().UserId, false);
                    if (!AllowAccess) continue;

                    bool AllowMan = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, ExamSegmentPermission.StaticId, ExamSegmentPermission.funcManage, ExamSegmentId, WebSessionUtility.GetCurrentLoginUser().UserId, false);

                    string ExamSegmentName = (string)dtExamSegment.Rows[iIndex]["ExamSegmentName"];
                    DateTime FromDate = (DateTime)dtExamSegment.Rows[iIndex]["FromDate"];
                    DateTime ToDate = (DateTime)dtExamSegment.Rows[iIndex]["ToDate"];
                    int ItemActive = (int)dtExamSegment.Rows[iIndex]["Active"];
                    string Button = "";
                    if (ItemActive == 0)
                    {
                        Button = GetSetActiveButton(SiteId, ExamSegmentId);
                    }
                    else
                    {
                        Button = GetSetInActiveButton(SiteId, ExamSegmentId);
                    }
                    RetHtml.Append(
                    "       <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                    "           <td class=tdIndex>" + (iIndex + 1) + "</td>" +
                    "           <td>" + ExamSegmentName + "</td>" +
                    "           <td class=tdDate>" + FromDate.ToString("dd/MM/yyyy") + "</td>" +
                    "           <td class=tdDate>" + ToDate.ToString("dd/MM/yyyy") + "</td>");
                    if (AllowMan)
                    {
                        RetHtml.Append(
                            "           <td class=tdButton><input type=button class=button value=\"Sửa\" onclick=\"javascript:ClientUpdateExamSegment('" + ExamSegmentId + "');\"></td>" +
                            "           <td class=tdButton><input type=button class=button value=\"Xóa\" onclick=\"javascript:ClientDeleteExamSegment('" + ExamSegmentId + "');\"></td>" +
                            "           <td class=tdButton><span id=\"spanButton" + ExamSegmentId + "\">" + Button + "</span></td>");
                    }
                    else
                    {
                        RetHtml.Append(
                            "           <td class=tdButton></td>" +
                            "           <td class=tdButton></td>" +
                            "           <td class=tdButton></td>");
                    }
                    RetHtml.Append(
                        "       </tr>");
                }
                RetHtml.Append(
                    "   </table>\r\n" +
                    "</div>\r\n");
                OAjaxOut.HtmlContent = RetHtml.ToString();

            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static string GetSetActiveButton(string SiteId, string ExamSegmentId)
        {
            string Html =
                "<input type=button class=button value=\"Sử dụng\" onclick=\"javascript:ClientStatus('" + ExamSegmentId + "',1);\">";
            return Html;
        }

        [AjaxPro.AjaxMethod]
        public static string GetSetInActiveButton(string SiteId, string ExamSegmentId)
        {
            string Html =
                "<input type=button class=button value=\"Hoàn thành\" onclick=\"javascript:ClientStatus('" + ExamSegmentId + "',0);\">";
            return Html;
        }

        [AjaxPro.AjaxMethod]
        public string GetAddExamUrl(string SiteId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                return "AddExamSegment.aspx";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg(string SiteId)
        {
            return "Bạn đã chắc chắn xóa chưa?";
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideCheckDeleteExamSegment(string SiteId, string ExamSegmentId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteExamSegment(string SiteId, string ExamSegmentId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                new ExamSegmentBll().DeleteExamSegment(null, SiteId, ExamSegmentId);
                return "Xóa thành công";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetUpdateExamUrl(string SiteId, string ExamSegmentId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                string UpdateUrl = "UpdateExamSegment.aspx?ExamSegmentId=" + ExamSegmentId;
                return UpdateUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {

            return "Đang đọc dữ liệu từ máy chủ";
        }

        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideSetStatus(
            string SiteId,
            string ExamSegmentId,
            int iActive)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                new ExamSegmentBll().SetStatus(null, SiteId, ExamSegmentId, iActive);
                if (iActive == 1)
                {
                    OAjaxOut.HtmlContent = GetSetInActiveButton(SiteId, ExamSegmentId);
                }
                if (iActive == 0)
                {
                    OAjaxOut.HtmlContent = GetSetActiveButton(SiteId, ExamSegmentId);
                }
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

    }
}
